<?php

/**
 * course actions.
 *
 * @package    samsung
 * @subpackage course
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class courseActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeList(sfWebRequest $request) {
    $this->courses = COURSETable::getInstance()->getCourses(true);
    $this->arrCategoryList  = CATEGORYTable::getInstance()->getCategoryArray();
    $this->arrStatusList    = StringUtil::getStatusArray();
  }

  public function executeAdd(sfWebRequest $request) {
    $errorCollector = array();
    $this->form = new CourseForm();
    $this->isEdit = false;
    
    if ($request->hasParameter('id')) {
      $this->isEdit = true;
      $course = COURSETable::getInstance()->find($request->getParameter('id'));
      $this->form->populateData($course);
      if (!$course instanceof COURSE) {
        $this->isEdit = false;
      }
    }
    try {
      if ($request->isMethod('post')) {
        $formData = $request->getParameter(CourseForm::FORM_NAME);
        $this->form->bind($formData);

        if ($this->form->isValid()) {
          if (!$this->isEdit) {
            $course = new COURSE();
          }
          $course->setTitle($formData[CourseForm::FIELD_TITLE]);
          $course->setDescription($formData[CourseForm::FIELD_DESCRIPTION]);
          $course->setCategoryId($formData[CourseForm::FIELD_CATEGORY]);
          $course->setIsActive($formData[CourseForm::FIELD_STATUS]);
          $course->save();

          $this->redirect('@course-list');
        }
      }
    } catch (Exception $e) {
      $errorCollector[] = 'Internal error!.. Please try again';
    }
    $this->errorCollector = $errorCollector;
  }

  public function executeDelete(sfWebRequest $request) {

    $course = COURSETable::getInstance()->find($request->getParameter('id'));
    if (!$course instanceof COURSE) {
      $this->forward404();
    }
    try {
      $course->setIsActive(Constants::STATUS_DELETED);
      $course->save();

      $this->redirect('@course-list');
    } catch (Exception $e) {

    }
  }
  
}
